package com.xxcloud.search.etl.note.impl;

import java.util.ArrayList;
import java.util.List;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.xxcloud.dao.dto.xxcloud.Note;
import com.xxcloud.dao.dto.xxcloud.NoteExample;
import com.xxcloud.dao.mapper.xxcloud.NoteMapper;
import com.xxcloud.dao.plugin.Limit;
import com.xxcloud.search.etl.note.INoteEtlService;
import com.xxcloud.search.etl.note.bean.NoteEtlQuery;
import com.xxcloud.search.model.note.NoteModel;

/**
 * Created by chenbingwen on 19/10/29.
 */
@Service("noteEtlService")
public class NoteEtlServiceImpl implements INoteEtlService {

    @Autowired
    private NoteMapper noteMapper;

    @Override
    public int count(NoteEtlQuery query) {
        NoteExample example = createNoteExample(query);
        return noteMapper.countByExample(example);
    }

    @Override
    public List<NoteModel> getNoteModels(NoteEtlQuery query) {
        List<NoteModel> result = new ArrayList<>();
        NoteExample example = createNoteExample(query);
        example.setLimit(new Limit(query.getFirst(), query.getMax()));
        List<Note> notes = noteMapper.selectByExampleWithBLOBs(example);
        if (CollectionUtils.isNotEmpty(notes)) {
            notes.stream().forEach(note -> {
                NoteModel noteModel = new NoteModel();
                BeanUtils.copyProperties(note, noteModel);
                result.add(noteModel);
            });
        }
        return result;
    }

    private NoteExample createNoteExample(NoteEtlQuery query) {
        NoteExample example = new NoteExample();
        NoteExample.Criteria criteria = example.createCriteria();
        List<Long> noteIds = query.getNoteIds();
        if (CollectionUtils.isNotEmpty(noteIds)) {
            criteria.andIdIn(noteIds);
        }
        return example;
    }

}
